import pandas as pd
from django.http import HttpResponse,JsonResponse
from django.apps import apps
from sklearn.decomposition import PCA

from .charts import data_all
from .models import *
from db.views import bp, pca
from .selenium_chongqing import diff_page


import time

import pandas as pd
from lxml import etree
from selenium.webdriver import Chrome           # 引入selenium浏览器自动框架、选择谷歌浏览器chrome类
from selenium.webdriver.chrome.service import Service       # 防止被反扒，
from selenium.webdriver.chrome.options import Options           # 隐藏浏览器痕迹的参数
from selenium.webdriver.common.by import By         # 定位标签by
from selenium.webdriver.common.action_chains import ActionChains            # 动作链条，拖动，下拉什么的
import json
from pprint import pprint
from random import randint
from lxml import etree
import pymysql


def test(request):
    # apps.get_models()  # 获取所有的models，包含Django自带的
    # apps.get_app_config('db')  # 获取blog的配置
    # apps.get_app_config('db').models
    # apps.get_app_config('db').get_models()  # 获取所有的models

    # llable = ['id','city','gdp', 'per_income', 'cpi', 'eva',
    #           'id','city','dirty_water', 'water_resource', 'forest_area', 'forest_cover',
    #           'id', 'city','raw_coal','natural_gas','oil','carbon_dioxide',
    #           'id', 'city','energy_c', 'coal_c', 'gas_c', 'oil_c', 'electricity_c']
    # res1 = []
    # res2 = []
    # mods = list(apps.get_app_config('db').get_models())
    # # 遍历所有的models
    # for m in mods:
    #     # 获取字段名
    #     tables = m._meta.fields
    #     table = [tables[t].name for t in range(len(tables))]
    #     res1.extend([m.objects.filter(city='重庆').values_list(i, flat=True) for i in table])
    #     res2.extend([m.objects.filter(city='成都').values_list(i, flat=True) for i in table])
    # print(table, len(table))
    # print(res1, len(res1))
    # pd1 = pd.DataFrame(data=res1, index=llable)
    # print(pd1.T)

    # print(pd1.T[['raw_coal', 'natural_gas', 'oil', 'carbon_dioxide']])

    # d = pd1.T[['raw_coal', 'natural_gas', 'oil', 'carbon_dioxide']]
    # r = pca(d)
    # print(r)
    # p1 = pca(pd1.T[['raw_coal', 'natural_gas', 'oil', 'carbon_dioxide']])
    # p2 = pca(pd2.T[['raw_coal', 'natural_gas', 'oil', 'carbon_dioxide']])

    # r = bp(pd1.T,p1)
    # print(r)
    # bp没有调试完

    # t = ['city','gdp','per_income','cpi','eva']
    # r = [economy.objects.filter().values_list(i, flat=True) for i in t]
    # df = pd.DataFrame(data=r, index=t)
    # df = df.T
    # # 添加时间
    # # df['date'] = [2017]
    # # df.set_index("date")
    # # 数据处理部分
    #
    #
    #
    # # 返回值部分

    # llable = ['id', 'city', 'gdp', 'per_income', 'cpi', 'eva',
    #           'id', 'city', 'dirty_water', 'water_resource', 'forest_area', 'forest_cover',
    #           'id', 'city', 'raw_coal', 'natural_gas', 'oil', 'carbon_dioxide',
    #           'id', 'city', 'energy_c', 'coal_c', 'gas_c', 'oil_c', 'electricity_c']
    # mods = list(apps.get_app_config('db').get_models())
    # res = []
    # for m in mods:
    #     # 获取字段名
    #     tables = m._meta.fields
    #     table = [tables[t].name for t in range(len(tables))]
    #     res.extend([m.objects.filter().values_list(i, flat=True) for i in table])
    # # print(table, len(table))
    # # print(res1, len(res1))
    # pd1 = pd.DataFrame(data=res, index=llable)
    # df = pd1.T
    # data = df[['dirty_water', 'water_resource', 'forest_area', 'forest_cover']]

    # data = data_all().iloc[:,1:6]
    # print(data)
    # print(data[data['city']=='重庆']['gdp'])
    # d1 = data[data['city']=='重庆']['gdp']
    # d2 = data[data['city'] == '成都']['gdp']



    # diff_page()  # 获取重庆各个参数的，函数....
    # print(len(diff_page()))
    df = diff_page()
    for d in df.itertuples():
        economy.objects.create(gdp = d.gdp,per_income = d.per_income,cpi = d.cpi,eva = d.eva,city = 'chongqing')




    # print([{'value':v,'name':k} for k,v in d1.to_dict(orient ='records')[0].items()][1:])






















    res = 0
    return JsonResponse({'message':res})